package org.example.cet.demos.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.example.cet.demos.domain.PageBean;
import org.example.cet.demos.domain.dto.SubDto;
import org.example.cet.demos.domain.dto.SubHomeworkDto;
import org.example.cet.demos.domain.dto.SubPaperDto;
import org.example.cet.demos.domain.dto.SubmissionDto;
import org.example.cet.demos.domain.po.Submission;
import org.example.cet.demos.domain.po.Teacher;
import org.example.cet.demos.domain.query.SubmissionQuery;
import org.example.cet.demos.mapper.SubmissionMapper;
import org.example.cet.demos.service.ISubmissionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author hq
 * @since 2024-12-18
 */
@Service
@RequiredArgsConstructor
public class SubmissionServiceImpl extends ServiceImpl<SubmissionMapper, Submission> implements ISubmissionService {

    @Autowired
    private SubmissionMapper submissionMapper;
    public PageBean getPage(SubmissionQuery submissionQuery){
        Page<Submission> page=new Page<>(submissionQuery.getCurrentPage(), submissionQuery.getPageSize());
        lambdaQuery()
                .like(!StrUtil.isBlankIfStr(submissionQuery.getTitle()), Submission::getTitle, submissionQuery.getTitle())
                .like(!StrUtil.isBlankIfStr(submissionQuery.getBankId()), Submission::getBankId, submissionQuery.getBankId())
                .eq(submissionQuery.getStuId() != null, Submission::getStuId, submissionQuery.getStuId())
                .like(!StrUtil.isBlankIfStr(submissionQuery.getQuestionId()), Submission::getQuestionId, submissionQuery.getQuestionId())
                .like(!StrUtil.isBlankIfStr(submissionQuery.getAnswer()), Submission::getAnswer, submissionQuery.getAnswer())
                .like(!StrUtil.isBlankIfStr(submissionQuery.getResult()), Submission::getResult, submissionQuery.getResult())
                .eq(submissionQuery.getHomeworkId() != null, Submission::getHomeworkId, submissionQuery.getHomeworkId())
//                .eq(submissionQuery.getStartTime() != null, Submission::getStartTime, submissionQuery.getStartTime())
//                .like(!StrUtil.isBlankIfStr(submissionQuery.getSubFinalTime()),Submission::getSubFinalTime,submissionQuery.getSubFinalTime())
                .page(page);
        PageBean pageBean=new PageBean();
        pageBean.setTotal(page.getTotal());
        pageBean.setRows(page.getRecords());
        return pageBean;
    }

//    @Override
//    public Map<String, Object> addSubmission(Submission submission) {
//        Map<String, Object> result = new HashMap<>();
//        submissionMapper.insert(submission);
//        return result;
//    }

//    public void update(Long id, String answer) {
//        submissionMapper.update(id, answer);
//    }
    public List<SubmissionDto> getList(Integer type){
        List<SubmissionDto> list = submissionMapper.getList(type);
        HashSet<String> set = new HashSet<>();
        ArrayList<SubmissionDto> list1 = new ArrayList<>();
        if (type == 2){
            list.forEach(x ->{
                if (!set.contains(x.getBankId())) {
                    list1.add(x);
                    set.add(x.getBankId());
                }
            });
            return list1;
        }
    return list;
}

    public List<SubDto> findId(Long id) {
        return submissionMapper.findId(id);
    }
    public List<SubHomeworkDto> findHomework(Long homeworkId) {
        return submissionMapper.findHomework(homeworkId);
    }
    public List<SubPaperDto> findPaper(String bankId) {
        return submissionMapper.findPaper(bankId);
    }



}
